package com.zntg.utils.poiTl;
import com.deepoove.poi.XWPFTemplate;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: spring_clould_ylcaifu_v1-master
 * @description: Poi_tl模板引擎
 * @author: zhengyunfei
 * @create: 2019-11-05 09:30
 **/
public class PoiTlUtil {
    public static void main(String args[]) {
        Map<String,Object> queryMap=new HashMap<>();
        queryMap.put("C_CUSTNAME","北京耀莱财富");
        XWPFTemplate template = XWPFTemplate.compile("D:\\fundcrm\\template\\rengoushu\\认购协议书模板-浙江贵直股权投资基金管理有限公司.docx").render(queryMap);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("D:\\apache-tomcat-7.0.68\\accessory\\out_template.docx");
            template.write(out);
            out.flush();
            out.close();
            template.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
    * @Description: 根据模板生成认购书
    * @Param: sourcePath 认购书模板路径 例如：D:\\word\\a.docx
    * @Param: targetPath 替换变量后的模板生成路径 例如：D:\\new\\create.docx
    * @Param: data 要替换的变量和内容对应关系，模板里面的变量命名{{a1}}{{a2}},则map.put('a1',"替换内容");
    * @return: void
    * @Author: zhengyunfei
    * @Date: 2019/11/5
    * @time:10:04
    */
    public void createTemplate(String sourcePath,String targetPath,Map<String,Object> data){
        XWPFTemplate template = XWPFTemplate.compile(sourcePath).render(data);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(targetPath);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
